package org.shop.controller.user;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.shop.dao.impl.UserInfoDaoImpl;
import org.shop.entity.UserInfo;
import org.shop.util.PagerUtil;
import org.shop.vo.ErrorVO;

/**
 * 分页 + 条件查询用户
 * 
 * @author Administrator
 * 
 */
@WebServlet("/admin/queryAllUserInfoByPagerAndCond.do")
public class QueryAllUserInfoByPagerAndCondServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

		// 1.获取当前页和查询条件
		String cpStr = req.getParameter("cp");
		String cond = req.getParameter("cond");

		// 2.验证数据的合法性
		cpStr = cpStr == null ? "1" : cpStr;
		cond = cond == null ? "" : cond;

		if (!cpStr.matches("\\d{1,}")) {
			ErrorVO error = new ErrorVO("错误的参数", "queryAllUserInfoByPager.do");
			req.setAttribute("error", error);
			req.getRequestDispatcher("../error.jsp").forward(req, resp);
			return;
		}

		int cp = Integer.parseInt(cpStr);

		// 3.分页 + 条件查询
		PagerUtil<UserInfo> pager = new UserInfoDaoImpl().selectAllUserByPagerAndCond(cp, 3,cond);

		// 4.把数据设置在作用域对象中
		req.setAttribute("pager", pager);

		// 5.页面跳转
		req.getRequestDispatcher("listAllUserinfoByPagerAndCond.jsp").forward(req, resp);

	}

}
